<Project Sdk="Microsoft.Build.Traversal">
  <ItemGroup>
    <ProjectReference Include="$(MSBuildThisFileDirectory)**\*.csproj" />

    <!-- These projects do not need to be built during source-build. -->
    <!-- For example, they may take dependencies on pre-built packages that aren't build-from-source. e.g. 'Microsoft.DotNet.CilStrip.Sources' -->
    <ProjectReference Remove="MonoTargetsTasks\MonoTargetsTasks.csproj;
                              MonoTargetsTasks\ILStrip\AssemblyStripper\AssemblyStripper.csproj"
                      Condition="'$(DotNetBuildSourceOnly)' == 'true'" />
  </ItemGroup>

  <!--
    Use synthetic inputs/outputs to avoid building it all the time. This should let devs build with
    MSBuild node reuse enabled (the Arcade default). If it were built every time, it would hit file
    locking issues vs. the persistent nodes that loaded the task DLL for the previous build. It
    isn't particularly accurate, but better than nothing.
  -->
  <Target Name="BuildIncrementally"
          DependsOnTargets="GetTasksSrc"
          Inputs="@(TasksSrc)"
          Outputs="$(TasksIntermediateFile)">
    <ItemGroup>
      <TaskProject Include="$(MSBuildProjectFullPath)" />
    </ItemGroup>

    <MSBuild Projects="@(TaskProject)"
             Properties="Configuration=$(TasksConfiguration);LibrariesConfiguration=$(LibrariesConfiguration);Platform=AnyCPU"
             Targets="Build" />

    <WriteLinesToFile File="$(TasksIntermediateFile)"
                      Lines="$(TasksIntermediateFile)"
                      Overwrite="true" />
  </Target>

  <Target Name="GetTasksSrc"
          DependsOnTargets="PrepareProjectReferences">
    <PropertyGroup>
      <TasksIntermediateFile>$([MSBuild]::NormalizePath('$(ArtifactsObjDir)', '$(MSBuildProjectName)', '$(TasksConfiguration)', 'build-semaphore.txt'))</TasksIntermediateFile>
    </PropertyGroup>

    <!-- Include both the project file and its sources as an input. -->
    <ItemGroup>
      <TasksSrc Include="%(ProjectReferenceWithConfiguration.RelativeDir)%(ProjectReferenceWithConfiguration.RecursiveDir)**\*" />
    </ItemGroup>
  </Target>
</Project>
